﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace h2100
{
    public class hMenu : hDialog
    {
        private hMenuItem[] _items;
        private int _nitem;
        private int _selectedIndex=-1;
        private int _hightlightedIndex=-1;
        private hGlobal.Command  _selectedCmd = hGlobal.Command.Undefined;
        protected hText _console;
        private Texture2D txtBackGround;

        public int SeletedIndex
        {
            get { return _selectedIndex; }
            set
            {
                if (value >= 0 && value < _nitem)
                    _selectedIndex = value;
                else
                    _selectedIndex = 0;
            }
        }

        public hText Console
        {
            set { _console = value; }
            get { return _console; }
        }

        public hGlobal.Command SelectedCommand
        {
            get { return _selectedCmd; }
            set { _selectedCmd = value; }
        }

        public hMenu()
        {

        }

        public void createFirstMenu(GraphicsDeviceManager graphics, ContentManager content, hText console)
        {
            txtBackGround = content.Load<Texture2D>(hGlobal.PathGui + "Wallpaper"); 

            Console = console;

            // Các item của menu            
            _nitem = 5;
            _items = new hMenuItem[_nitem];

            for (int i = 0; i < _nitem; i++)
            {
                _items[i] = new hMenuItem(content, 0, 0);
                _items[i].Console = this.Console;
            }

            // Cấu hình menu
            create1SpriteObject(content, hGlobal.PathGui + hGlobal.MenuBackground);
            //_nsprite = 1;
            //Texture2D texture;
            //texture = content.Load<Texture2D>(hGlobal.PathGui+ hGlobal.MenuBackground);
            //_sprite = new hSprite[1];
            //_sprite[0] = new hSprite(1);
            //_sprite[0].setTexture(texture, 0);
            //_sprite[0].setSize(300, 400);

            X = graphics.GraphicsDevice.Viewport.Width / 2 - _sprite[0].Width / 2;
            Y = graphics.GraphicsDevice.Viewport.Height / 2 - _sprite[0].Height / 2;

            // Chỉnh vị trí các item
            for (int i = 0; i < _nitem; i++)
            {
                _items[i].X = X + (_sprite[0].Width - _items[i].Width) / 2;
                _items[i].Y = 10 + Y + i * (_sprite[0].Height / _nitem);
            }
           
            _items[0].Text = "Play";
            _items[0].Command = hGlobal.Command.Play;
            _items[1].Visible = false;
            _items[2].Visible = false;
            _items[3].Text = "Credits";
            _items[3].Command = hGlobal.Command.Credits;
            _items[4].Text = "Quit";
            _items[4].Command = hGlobal.Command.Quit;

            isMoving = true;
        }

        public void createPauseMenu(GraphicsDeviceManager graphics, ContentManager content, hText console)
        {
            Console = console;

            // Các item của menu            
            _nitem = 5;
            _items = new hMenuItem[_nitem];

            for (int i = 0; i < _nitem; i++)
            {
                _items[i] = new hMenuItem(content, 0, 0);
                _items[i].Console = this.Console;
            }

            // Cấu hình menu
            create1SpriteObject(content, hGlobal.PathGui + hGlobal.MenuBackground);
            //_nsprite = 1;
            //Texture2D texture;
            //texture = content.Load<Texture2D>(hGlobal.PathGui + hGlobal.MenuBackground);
            //_sprite = new hSprite[1];
            //_sprite[0] = new hSprite(1);
            //_sprite[0].setTexture(texture, 0);
            //_sprite[0].setSize(300, 400);

            X = graphics.GraphicsDevice.Viewport.Width / 2 - _sprite[0].Width / 2;
            Y = graphics.GraphicsDevice.Viewport.Height / 2 - _sprite[0].Height / 2;

            // Chỉnh vị trí các item
            for (int i = 0; i < _nitem; i++)
            {
                _items[i].X = X + (_sprite[0].Width - _items[i].Width) / 2;
                _items[i].Y = 10 + Y + i * (_sprite[0].Height / _nitem);
            }

            _items[0].Text = "Main menu";
            _items[0].Command = hGlobal.Command.ReturnToMainMenu;
            _items[1].Visible = false;
            _items[2].Visible = false;
            _items[3].Visible = false;
            _items[4].Text = "Continue";
            _items[4].Command = hGlobal.Command.Continue;

            isMoving = true;
        }

        public void createEndGameMenu(GraphicsDeviceManager graphics, ContentManager content, hText console)
        {
            txtBackGround = content.Load<Texture2D>(hGlobal.PathGui + "EndWallpaper");

            Console = console;

            // Các item của menu            
            _nitem = 5;
            _items = new hMenuItem[_nitem];

            for (int i = 0; i < _nitem; i++)
            {
                _items[i] = new hMenuItem(content, 0, 0);
                _items[i].Console = this.Console;
            }

            // Cấu hình menu
            create1SpriteObject(content, hGlobal.PathGui + hGlobal.MenuBackground);
            //_nsprite = 1;
            //Texture2D texture;
            //texture = content.Load<Texture2D>(hGlobal.PathGui + hGlobal.MenuBackground);
            //_sprite = new hSprite[1];
            //_sprite[0] = new hSprite(1);
            //_sprite[0].setTexture(texture, 0);
            //_sprite[0].setSize(300, 400);

            X = graphics.GraphicsDevice.Viewport.Width / 2 - _sprite[0].Width / 2;
            Y = graphics.GraphicsDevice.Viewport.Height / 2 - _sprite[0].Height / 2;

            // Chỉnh vị trí các item
            for (int i = 0; i < _nitem; i++)
            {
                _items[i].X = X + (_sprite[0].Width - _items[i].Width) / 2;
                _items[i].Y = 10 + Y + i * (_sprite[0].Height / _nitem);
            }

            _items[0].Text = "The end!";
            _items[0].Command = hGlobal.Command.Undefined;
            _items[1].Visible = false;
            _items[2].Visible = false;
            _items[3].Visible = false;
            _items[4].Text = "Main menu";
            _items[4].Command = hGlobal.Command.ReturnToMainMenu;

            isMoving = true;
        }

        public int nDelay = 1;
        protected int iDelay = 0;

        private bool _isMoving;
        private bool isMoving
        {
            get { return _isMoving; }
            set
            { 
                _isMoving = value;
                if (_isMoving == true)
                {
                    tempY = Y;
                    for (int i = 0; i < _nitem; i++)
                    {
                        _items[i].Y -= Y;
                    }
                    Y = 0; 
                }
            }
        }

        //private float tempX=0;
        private float tempY=0;

        public override void update(hParams args, GameTime gameTime)
        {
            _selectedCmd = hGlobal.Command.Undefined;
            _selectedIndex = -1;
            _hightlightedIndex = -1;

            if (isMoving)
            {
                Y += 4f;

                for (int i = 0; i < _nitem; i++)
                {
                    _items[i].Y += 4f;
                }

                if (Y >= tempY)
                {
                    tempY = 0;
                    isMoving=false;
                }

                return;
            }            

            for (int i = 0; i < _nitem; i++)
            {
                _items[i].update(args, gameTime);

                if (_items[i].Hightlighted)
                {
                    _hightlightedIndex = i;
                }

                if (_items[i].Selected)
                {
                    _selectedIndex = i;
                    _selectedCmd = _items[i].Command;
                }                
            }

            switch (_selectedCmd)
            {
                case hGlobal.Command.Quit:
                    args.GameState = hGlobal.GameState.Exit;
                    isMoving = true;
                    break;
                case hGlobal.Command.Play:
                    args.GameState = hGlobal.GameState.LoadGame;
                    isMoving = true;
                    break;
                case hGlobal.Command.Credits:                    
                    args.GameState = hGlobal.GameState.Credits;
                    isMoving = true;
                    break;
                case hGlobal.Command.ReturnToMainMenu:
                    args.GameState = hGlobal.GameState.FirstMenu;
                    isMoving = true;
                    break;
                case hGlobal.Command.Continue:
                    args.GameState = hGlobal.GameState.Playing;
                    isMoving = true;
                    break;
                default:
                    break;
            }

            //KeyboardState keyState = Keyboard.GetState();

            //if (iDelay == 0)
            //{
            //    nDelay = 1;
            //    if (keyState.IsKeyDown(Keys.Down))
            //    {
            //        _hightlightedIndex = (_hightlightedIndex + 1) % _nitem;
            //        hightlightItem(_hightlightedIndex);
            //        nDelay = 10;
            //    }
            //    if (keyState.IsKeyDown(Keys.Up))
            //    {
            //        _hightlightedIndex = (_hightlightedIndex + _nitem -1) % _nitem;
            //        hightlightItem(_hightlightedIndex);
            //        nDelay = 10;
            //    }
            //    if (keyState.IsKeyDown(Keys.Enter))
            //    {
            //        selectItem(_hightlightedIndex);
            //        nDelay = 10;
            //    }
            //}
            //iDelay = (iDelay + 1) % nDelay;            
        
        }

        public void draw(GameTime gtime, SpriteBatch spriteBatch, hParams args)
        {
            if (args.GameState == hGlobal.GameState.FirstMenu || args.GameState == hGlobal.GameState.End)
            {
                spriteBatch.Draw(txtBackGround, new Vector2(0, 0), Color.White);
            }

            base.draw(gtime, spriteBatch,200);
            for(int i=0;i<_nitem;i++)
            {
                _items[i].draw(gtime, spriteBatch, 200);
                // đổ bóng cho text
                Console.print(gtime, spriteBatch, _items[i].X+2, _items[i].Y+2, _items[i].Width, _items[i].Height, _items[i].Text, Color.Black, hGlobal.FontStyle.Bold);
                // text thật
                Console.print(gtime, spriteBatch, _items[i].X, _items[i].Y, _items[i].Width, _items[i].Height, _items[i].Text, Color.Yellow,hGlobal.FontStyle.Bold);
            }
            
        }

    }
}
